package com.usian.login.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.pojo.ResultVo;
import com.usian.common.pojo.User;
import com.usian.login.dao.LoginDao;
import com.usian.login.dao.UserDao;
import com.usian.login.service.LoginService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
   private LoginDao loginDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo sendCode(String phone) {

//        1、判断手机号是否正确
        boolean flag = Validator.isMobile(phone);
//         2、判断手机号是否已经注册
        if (flag){
            Integer count=loginDao.getUserCount(phone);
            if (count>0){
//                手机号已经注册，发送验证码
                rabbitTemplate.convertAndSend("java-exchange","java-queue",phone);
//                AliCodeUtils aliCodeUtils=new AliCodeUtils();
//                aliCodeUtils.sendLoginCode(phone,"123456");
//                stringRedisTemplate.opsForValue().set(phone,"123456");
//                stringRedisTemplate.expire(phone,5, TimeUnit.MINUTES);
                return new ResultVo(200,"验证码发送成功",null);
            }else {
                return new ResultVo(201,"此手机号不存在，需要激活",null);
            }
        }else {
            return new ResultVo(303,"手机号错误",null);
        }

    }

    /*
    手机号验证码登录
    * */
    @Override
    public ResultVo userLogin(String phone, String code) {
//        1、判断手机号和验证码是否填写
        Boolean isPhone = StrUtil.isEmpty(phone);
        boolean isCode = StringUtils.isEmpty(code);
        if (!isPhone){
            if (!isCode){
                //        2、判断用户是否存在
//                int count=loginDao.getUserCount(phone);
                LambdaQueryWrapper<User>lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(User::getPhone,phone);
                User user=userDao.selectOne(lambdaQueryWrapper);
                if (user!=null){
                    //        3、验证码是否正确
                    String redisCode=stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            //        4、正确返回用户信息
                            return new ResultVo(200,"登录成功",user );
                        }else {
                            return new ResultVo(303,"验证码输入错误，请重新输入",null );
                        }
                    }else {
                        return new ResultVo(303,"验证码过期，需要重新发送",null );

                    }

                }else {
                    return new ResultVo(303,"手机号不存在，需要注册",null );
                }
            }else {
                return new ResultVo(303,"必须填写验证码",null );
            }
        }else {
            return new ResultVo(303,"手机号是必填",null );
        }


    }

    @Override
    public ResultVo save(User user) {
        userDao.insert(user);
        return new ResultVo(200,"保存成功",null);
    }
}
